from django.shortcuts import render
from rest_framework.generics import ListAPIView
from .serializers import CourseCategoryModelSerializer, CourseModelSerializer
from .models import CourseCategory, Course
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import OrderingFilter
from .paginations import CustomPageNumberPagination


class CourseCategoryListAPIView(ListAPIView):
    '''获取课程分类列表'''
    queryset = CourseCategory.objects.filter(is_show=True, is_delete=False).order_by('orders')
    serializer_class = CourseCategoryModelSerializer


class CourseListAPIView(ListAPIView):
    '''获取课程列表'''
    queryset = Course.objects.filter(is_show=True,is_delete=False).order_by("orders")
    serializer_class = CourseModelSerializer
    # 按条件筛选[分类]展示课程信息
    filter_backends = [DjangoFilterBackend, OrderingFilter]
    # 设置支持设置的筛选过滤字段
    filter_fields = ['course_category']
    # 设置支持设置的排序字段
    ordering_fields = ('id', 'students', 'price')
    # 指定分页器
    pagination_class = CustomPageNumberPagination


from .serializers import CourseDetailModelSerializer
from rest_framework.generics import RetrieveAPIView
class CourseRetrieveAPIView(RetrieveAPIView):
    queryset = Course.objects.filter(is_show=True, is_delete=False)
    serializer_class = CourseDetailModelSerializer


from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from luffyapi.libs.polyv import PolyvPlayer
from django.conf import settings
from rest_framework.response import Response
class PolyvAPIView(APIView):
    '''保利威视频视图'''
    permission_classes = [IsAuthenticated]
    def get(self, request):
        """获取保利威云视频加密播放的token"""
        """接受客户端的请求参数"""
        vid = request.query_params.get('vid')   # 视频播放id
        remote_addr = request.META.get('REMOTE_ADDR')   # 用户ip
        user_id = request.user.id   # 用户id
        user_name = request.user.username   # 用户名
        polyv = PolyvPlayer(
            settings.POLYV_CONFIG['userId'],
            settings.POLYV_CONFIG['secretkey'],
            settings.POLYV_CONFIG['tokenUrl'],
        )

        data = polyv.get_video_token(vid,remote_addr,user_id,user_name)

        return Response(data)


